// run: $exec < input
#include <cstdio>
#include <queue>
#include <vector>
#include <unordered_set>

void scan(int & a)
{
	a = 0;
	bool flag = false;
	char ch = std::getchar();
	if (ch == '-')
		flag = true;
	else if (ch >= '0' && ch <= '9')
		a = ch - '0';

	while ((ch = std::getchar()) >= '0' && ch <= '9')
		a = a * 10 + ch - '0';
	if (flag) a = -a;
}

int const maxn = 1000007;
int father[maxn];
int degree[maxn];
int n, m1, m2;

std::vector<std::vector<int>> g;

void init()
{
	g.clear();
	g.resize(n + 1);
	for (int i = 1; i <= n; i++) {
		father[i] = i;
		degree[i] = 0;
	}
}

int get_father(int x) { return x == father[x] ? x : father[x] = get_father(father[x]); }
void set_union(int x, int y)
{
	int tx = get_father(x);
	int ty = get_father(y);
	if (tx != ty) father[tx] = ty;
}

void add_edge(int u, int v)
{
	g[u].push_back(v);
	degree[v]++;
}

int main()
{
	int T; scan(T);
	while (T--) {
		scan(n); scan(m1); scan(m2);
		init();
		bool ok = false;
		for (int i = 0, x, y; i < m1; i++) {
			scan(x); scan(y);
			if (get_father(x) == get_father(y))
				ok = true;
			set_union(x, y);
		}

		for (int i = 1; i <= n; i++)
			father[i] = get_father(i);

		for (int i = 0, x, y; i < m2; i++) {
			scan(x); scan(y);
			add_edge(get_father(x), get_father(y));
		}

		if (ok) { printf("YES\n"); continue; }

		std::unordered_set<int> all;
		for (int i = 1; i <= n; i++)
			all.insert(father[i]);
		std::queue<int> pq;
		int tot = 0;
		for (auto i : all) {
			if (!degree[i]) pq.push(i);
			else tot++;
		}
		while (!pq.empty()) {
			int now = pq.front(); pq.pop();
			for (auto i : g[now]) {
				degree[i]--;
				if (!degree[i]) {
					pq.push(i);
					tot--;
				}
			}
		}
		if (tot) ok = true;
		if (ok) printf("YES\n");
		else printf("NO\n");
	}
}

